//
//  RHomeSubPageViewController.m
//  iPlan
//
//  Created by 侯伟 on 2021/8/24.
//

#import "RHomeSubPageViewController.h"
#import "RVideoItemView.h"
#import "RVideoObj.h"
#import "RHomeSearchBar.h"

#import "RAllVideoRequest.h"

#import "RHomeAllVideoViewController.h"
#import "RSearchViewController.h"

#import <MJRefresh/MJRefresh.h>

@interface RHomeSubPageViewController (){
    NSInteger pageIndex;
    NSInteger total;
}

@property(nonatomic,strong)UIScrollView *scrollView;

@property(nonatomic,strong)MyLinearLayout *contentLayout;

@property(nonatomic,strong)RHomeSearchBar *searchBar;

@property(nonatomic,strong)MyFlowLayout *flowLayout;

@property(nonatomic,strong)NSMutableArray* movies;

@end

@implementation RHomeSubPageViewController

- (NSMutableArray*)movies{
    if(!_movies){
        _movies = [NSMutableArray array];
    }
    return _movies;
}

- (void)loadView{
    [super loadView];
    
    MyLinearLayout *linearLayout = [MyLinearLayout linearLayoutWithOrientation:(MyOrientation_Vert)];
    linearLayout.backgroundColor = COLOR_WHITE;
    linearLayout.myMargin = 0;
    [self.view addSubview:linearLayout];
    
    RHomeSearchBar* searchBar = [RHomeSearchBar new];
    searchBar.VideoType = self.VideoType;
    searchBar.myMargin = 0;
    searchBar.myHeight = PX(50);
    [linearLayout addSubview:searchBar];
    
    //底部滚动控件
    UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:self.view.bounds];
    scrollView.showsHorizontalScrollIndicator = NO;
    scrollView.showsVerticalScrollIndicator = NO;
    scrollView.myMargin = 0;
    scrollView.weight = 1.0;
    [linearLayout addSubview:scrollView];
    _scrollView = scrollView;
    
    MJRefreshNormalHeader* header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [self requestData:Refresh];
    }];
    header.arrowView.image = [[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"arrow@2x" ofType:@"png"]] imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
    header.stateLabel.font = FONT(13);
    header.stateLabel.textColor = COLOR_GRAY_BASE;
    header.lastUpdatedTimeLabel.font = FONT(11);
    header.lastUpdatedTimeLabel.textColor = COLOR_GRAY_BASE;
    header.labelLeftInset = 16;
    scrollView.mj_header = header;
    
    MJRefreshAutoNormalFooter* footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        [self requestData:LoadMore];
    }];
    footer.automaticallyRefresh = NO;
    footer.stateLabel.font = FONT(13);
    footer.stateLabel.textColor = COLOR_GRAY_BASE;
    scrollView.mj_footer = footer;
    
    // 流式布局
    MyFlowLayout *flowLayout = [MyFlowLayout flowLayoutWithOrientation:MyOrientation_Vert arrangedCount:0];
    flowLayout.subviewVSpace = 10;
    flowLayout.subviewHSpace = 10;
    flowLayout.gravity = MyGravity_Horz_Fill;
    flowLayout.padding = UIEdgeInsetsMake(0, 16, 0, 16);
    flowLayout.myHorzMargin = 0;
    flowLayout.myTop = 10;
    [flowLayout setSubviewsSize:PX(105) minSpace:8 maxSpace:CGFLOAT_MAX centered:NO];
    flowLayout.myHeight = MyLayoutSize.wrap;
    [scrollView addSubview:flowLayout];
    _flowLayout = flowLayout;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self requestData:Refresh];
}

- (void)reloadData:(RefreshType)refreshType{
    
    if(refreshType == Refresh){
        [self.flowLayout removeAllSubviews];
    }
    
    for (int i = self.flowLayout.subviews.count; i < self.movies.count; i++) {
        RVideoObj* movie = self.movies[i];
        RVideoItemView* item = [RVideoItemView new];
        item.movie = movie;
        [self.flowLayout addSubview:item];
    }
    
    if(self.movies.count == total){
        [self.scrollView.mj_footer endRefreshingWithNoMoreData];
    }else{
        [self.scrollView.mj_footer resetNoMoreData];
    }
}

- (void)endRefresh{
    [self.scrollView.mj_header endRefreshing];
    [self.scrollView.mj_footer endRefreshing];
}

#pragma mark  -- request
- (void)requestData:(RefreshType)refreshType{

    NSInteger page = refreshType == Refresh ? 1 : pageIndex+1;
    
    RAllVideoRequest* request = [RAllVideoRequest new];
    request.c_id = self.VideoType;
    request.action = @"t_i_l";
    request.page = page;
    
    [RequestOperation encryptedRequest:request quite:YES callBack:^(HResponse * _Nullable response) {
        
        [self endRefresh];
        
        [response success:^(AllMovieList*  _Nonnull data) {
            
            self->total = data.total;
            self->pageIndex = data.current_page;
            
            if(refreshType == Refresh){
                [self.movies removeAllObjects];
            }
            [self.movies addObjectsFromArray:data.lists];
            
            [self reloadData:refreshType];
        }];
    }];
}

#pragma mark  --- action

/// 所有的
- (void)allMoive:(UIButton*)sender{
    RHomeAllVideoViewController* allMove = [RHomeAllVideoViewController new];
    allMove.VideoType = self.VideoType;
    [UIViewController showViewController:allMove animated:YES];
}

///搜索
- (void)searchMoive:(UIButton*)sender{
    [UIViewController showViewController:[RSearchViewController new] animated:YES];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
